const Router = require('../../Router');
const resContent = require('../../../../util/ResValue')
const Where = require('../../../../util/SqlWhere');
const Validation = require('../../../../util/Validation');
const dbUtil = require('../../../../util/dbutil');




class CityRevise extends Router {
    constructor() {
        //查询条件
        super('/super/city-revise', 'post', false);

        //所有字段验证
        this.addIntermediateKey(this.check);
        //城市验证
        this.addIntermediateKey(Validation.isCity('id'));
        //父级城市字段验证
        this.addIntermediateKey(Validation.isCitiesArePresence);
        //等级字段验证
        this.addIntermediateKey(Validation.isCityLevel);
    }


    async handle(ctx, next) {
        const body = ctx.request.body;
        //修改城市
        const reviseRes = await dbUtil.exec(`
            update city set name = ?,fatherId = ?,level = ?,code = ? where id = ?
            `, [body.name, body.fatherId, body.level, body.code, body.id]
        );
        ctx = await resContent.success(ctx);
        ctx.body.data = {
            modifyTheQuantity: reviseRes
        };
    }

    //检查所需字段
    async check(ctx, next) {
        const body = ctx.request.body;
        const isLack = await Validation.theFieldIsMissingExists(body, ['id','name', 'fatherId', 'level', 'code']);
        //有字段缺失
        if (isLack !== false) {
            ctx = await resContent.parametersAreMissing(ctx);
            ctx.body.message += `${isLack}`;
            return 0;
        }
        await next();
    }
}


module.exports = CityRevise;
